<template>
  <KView class="coupon-tao-data-view">
    <KView class="coupon-tao-data-view-content" :class="{'has-page':loading||hasNextData}">
      <coupon-item v-for="coupon in couponList" :key="`coupon_${coupon.itemId}`" :coupon="coupon" @collect-coupon="collectCoupon">
      </coupon-item>
    </KView>
    <KView class="coupon-tao-data-view-more" v-if="loading||hasNextData">
      <sys-loading v-if="loading" :size="60"></sys-loading>
      <KView v-else-if="hasNextData" class="coupon-tao-data-view-btn" @click="lookMore">查看更多</KView>
    </KView>
  </KView>
</template>

<script>
  import SysLoading from 'common/sysLoading';
  import CouponItem from './couponItem';

  export default {
    props: {
      couponList: {
        type: Array,
        required: true
      },
      pagination: {
        type: Object,
        required: true
      },
      nextDataFun: {
        type: Function,
        required: true
      }
    },

    computed: {
      hasNextData() {
        return this.pagination.hasNext;
      }
    },

    data() {
      return {
        loading: false,
        isMiniprogram: process.env.isMiniprogram
      };
    },

    methods: {
      collectCoupon(coupon) {
        let itemId = coupon.itemId;
        let url = `/couponTaoDetail/${itemId}`;
        if (this.isMiniprogram) {
          window.open(url);
        }
      },

      lookMore() {
        if (this.loading) {
          return;
        }

        let pagination = this.pagination;
        if (!pagination.hasNext) {
          return;
        }

        this.loading = true;
        this.nextDataFun().then(couponList => {
          this.loading = false;
        }).catch(() => {
          this.loading = false;
        });
      }
    },

    components: {
      SysLoading,
      CouponItem
    }
  };

</script>

<style lang="scss">
  .coupon-tao-data-view {
    padding: 0 formatPx(30);
    height: 100%;
    overflow: auto;

    .coupon-tao-data-view-content {
      margin-bottom: formatPx(40);

      &.has-page {
        margin-bottom: 0;
      }

      .coupon-tao-item {
        margin-bottom: formatPx(30);

        &:last-child {
          margin-bottom: 0;
        }
      }
    }

    .coupon-tao-data-view-more {
      position: relative;
      padding: formatPx(50) 0;
      height: formatPx(66);

      .coupon-tao-data-view-btn {
        text-align: center;
        width: formatPx(255);
        height: 100%;
        line-height: formatPx(65);
        border: 1px solid #999999;
        font-size: formatPx(30);
        margin: auto;
        border-radius: formatPx(33);
      }
    }
  }

</style>
